home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / SPR5 / CONVERT / SPR2XLIB.C < prev   
Encoding:
C/C++ Source or Header  |  1996-05-27  |  6.0 KB  |  234 lines

  1. #include "c:\xlib\xlib.h"
  2. #include "c:\xlib\xpoint.h"
  3. #include "c:\xlib\xrect.h"
  4. #include "c:\xlib\xpal.h"
  5. #include "c:\xlib\xpbitmap.h"
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <conio.h>
  9. #include <string.h>
  10. #include <alloc.h>
  11.  
  12. /* WGT sprite file to Xlib sprite file converter
  13.   
  14.  This program was compiled with xlib v3.  One bug was noticed in this
  15.  version:  The x_put_pix routine hangs the system.  I have drawn
  16.  a line to the same point instead and this works fine.   Themie has 
  17.  probably fixed this, since Xlib is currently at version 6. Therefore
  18.  you may need to change some of this code depending on what has 
  19.  changed in Xlib.
  20.  
  21.  
  22.  */
  23.  
  24.  
  25. void far *sprites[2001];
  26. void convertsprites(char *,char *);
  27. void loadsprites(char *,void far *loadspr[]);
  28. void testsprites();
  29. void freesprites(void far *freespr[]);
  30.  
  31. int main(void)
  32. {
  33.    /* Note that you may select whichever mode you please */
  34.    x_set_mode(X_MODE_320x480,320);
  35.    /*       mode,width of in pixels */
  36.  
  37.    convertsprites("sprtconv.spr","out.spr");
  38.    loadsprites("out.spr",sprites);
  39.    testsprites();
  40.    freesprites(sprites);
  41.  
  42.  
  43.    /* clean up */
  44.    x_text_mode();
  45.    return 0;
  46. }
  47.  
  48. void convertsprites(char *infile,char *outfile)
  49. {
  50. FILE *in;   /* 256 color sprite file */
  51. FILE *out;  /* converted sprite file */
  52. unsigned char palette[768];         /* 256 * 3 (RGB values) */
  53. int maxcolor;
  54. int maxsprite;
  55. long size;
  56. unsigned char far *temp;
  57. int a,b,i,j,spritemade;
  58. int col;
  59. char buf[14];
  60. int x,y;
  61. int startingsprite;
  62.  
  63.     /* Open the files */
  64.     if ((in = fopen (infile, "rb")) == NULL)
  65.        {
  66.        x_text_mode();
  67.        printf("Could not open 256 color sprite file");
  68.        exit(1);
  69.        }
  70.     if ((out = fopen (outfile, "wb")) == NULL)
  71.        {
  72.        x_text_mode();
  73.        printf("Could not open converted sprite file");
  74.        exit(1);
  75.        }
  76.  
  77.      fread(&a, 1, 2, in);
  78.      /* Get the version number, and change the startingsprite accordingly.
  79.      If version <= 3, maxsprite contains the maximum number of sprites
  80.      that can be stored in a file.  If version > 4, maxsprite contains
  81.      the number of the highest sprite in the file. (empty sprites at
  82.      the end are not kept in the file. */
  83.  if (a <= 3)
  84.    startingsprite = 1;
  85.  else startingsprite = 0;    /* Version 4 starts at sprite 0 */
  86.  
  87.     fread (buf, 1, 13, in); /* sprite header */
  88.     if (0 == strnicmp (" Sprite File ", buf, 13)) /* see if it is a sprite file */
  89.     {
  90.     fread(palette,1,768,in); /* Read in 256 color palette */
  91.     maxcolor=256;
  92.     putw(maxcolor,out);  /* Write the number of colors stored in file */
  93.  
  94.     for (i = 0; i < maxcolor; i++) /* read in the palette */
  95.         {
  96.        fputc(palette[i*3],out);    /* Write out Red */
  97.        fputc(palette[i*3+1],out);  /* Green */
  98.        fputc(palette[i*3+2],out);  /* And Blue color values */
  99.  
  100.         }
  101.         
  102.     maxsprite = getw (in);   /* maximum sprites in this file */
  103.     putw(maxsprite,out);
  104.     for (i = startingsprite; i <= maxsprite; i++) /* load them in */
  105.     {
  106.         spritemade = getw (in); /* flag to see if sprite exists */
  107.         putw(spritemade,out); 
  108.         if (spritemade == 1)
  109.         {
  110.         x_rect_fill(0,0,319,199,0,0); /* Clear the previous sprite */
  111.         a = getw (in); /* get width and height */
  112.         b = getw (in);
  113.         putw(a,out); /* Put width and height */
  114.         putw(b,out);
  115.  
  116.         /* Read in the image data. Each byte represents a color
  117.         from 0-255. Obviously converting sprites that use more colors
  118.         than the current mode allows will not work. Draw sprites using
  119.         only the first colors (eg 0-16), up to maxcolors of the mode you're using. */
  120.         for (y=0; y<b; y++)
  121.           for (x=0; x<a; x++)
  122.              {
  123.              col=fgetc(in);
  124.              x_line(x,y,x,y,col,0);
  125.              }
  126.  
  127.         size = a*b+2; /* get byte size of image */
  128.         if ((temp = farmalloc(size)) == NULL)
  129.             {
  130.               x_text_mode();
  131.               printf("Error: not enough heap space in convertsprites.\n");
  132.               exit(1);
  133.            }
  134.         x_get_pbm(0,0,a/4,b,0,temp); /* Get the image in new mode */
  135.         fwrite(temp,size,1,out); /* Write the data in getimage format */
  136.         farfree(temp);
  137.         }
  138.         }
  139.     }
  140.     fclose (in);
  141.     fclose (out);
  142. }
  143.  
  144. void loadsprites (char *infile, void far *loadspr[])
  145. {
  146. FILE *in;   /* converted color sprite file */
  147. int maxsprite;
  148. long size;
  149.  
  150. unsigned char *temp;
  151. int a,b,i,j,spritemade;
  152. char buf[14];
  153. int x,y,maxcolor;
  154. unsigned char pal[768];
  155.  
  156.     /* Open the files */
  157.     if ((in = fopen (infile, "rb")) == NULL)
  158.        {
  159.        closegraph();
  160.        printf("Could not load converted color sprite file");
  161.        exit(1);
  162.        }
  163.  
  164.     maxcolor=getw(in);
  165.  
  166.     for (i = 0; i < maxcolor; i++) /* read in the palette */
  167.        { 
  168.        pal[i*3]=fgetc(in);
  169.        pal[i*3+1]=fgetc(in);
  170.        pal[i*3+2]=fgetc(in);
  171.        }
  172.     x_put_pal_raw(pal,256,0);
  173.     
  174.     maxsprite = getw (in);   /* maximum sprites in this file */
  175.  
  176.     for (i = 0; i < maxsprite; i++) /* load them in */
  177.     {
  178.         spritemade = getw (in); /* flag to see if sprite exists */
  179.         if (spritemade == 1)
  180.         {
  181.         a = getw (in); /* get width and height */
  182.         b = getw (in);
  183.  
  184.         size = a*b+2; /* get byte size of image */
  185.         if ((loadspr[i] = farmalloc(size)) == NULL)
  186.             {
  187.               closegraph();
  188.               printf("Error: not enough heap space in loadsprites().\n");
  189.               freesprites(loadspr);
  190.               exit(1);
  191.            }
  192.         fread(loadspr[i],size,1,in);
  193.         } 
  194.         else loadspr[i]=NULL;
  195.     
  196.     }
  197.     fclose(in);
  198. }
  199.  
  200.  
  201.  
  202.  
  203. void testsprites(void)
  204. /* Loops through 10 sprites, displaying them on the screen
  205.    using the putimage method. Press a key to go to the next sprite. */
  206. {
  207. int i,j;
  208.  
  209. for (i=0; i<10; i++) 
  210.   {
  211.   x_rect_fill(0,0,ScrnPhysicalPixelWidth,ScrnPhysicalHeight,0,0);
  212.   
  213.    if (sprites[i] !=NULL)
  214.      {
  215.      for (j=1; j<20; j++)
  216.     x_put_masked_pbm(rand() % ScrnPhysicalPixelWidth,
  217.         rand() % ScrnPhysicalHeight,0,sprites[i]); 
  218.        /* Put the converted image on the screen */
  219.      getch();
  220.      }
  221.   }
  222. }
  223.  
  224. void freesprites(void far *freespr[])
  225. {
  226. int i;
  227.  
  228. for (i=0; i<2001; i++)
  229.   {
  230.   if (freespr[i] !=NULL)
  231.     farfree(freespr[i]);
  232.   }
  233. }
  234.